<template>
  <div v-loading="loading">
    <!--新增 编辑对话框-->
    <el-dialog
        title="提示"
        :close-on-click-modal=false
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose"
        ref="menuDialogForm">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">
        <el-row>
          <el-form-item label="上级菜单" prop="parentId">
            <el-select v-model="editForm.parentId" placeholder="请选择上级菜单" style="width: 100%">
              <template v-for="item in tableData">
                <el-option :label="item.name" :value="item.id"></el-option>
                <template v-for="child in item.children">
                  <el-option :label="child.name" :value="child.id">
                    <span>{{ "- " + child.name }}</span>
                  </el-option>
                </template>
              </template>
            </el-select>
          </el-form-item>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="菜单名称" prop="name" label-width="100px">
              <el-input v-model="editForm.name" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="权限编码" prop="perms" label-width="100px">
              <el-input v-model="editForm.perms" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="图标" prop="icon" label-width="100px">
              <el-input v-model="editForm.icon" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单URL" prop="path" label-width="100px"
                          :rules="this.editForm.type === 0?[{required: false}]:[{required: true,message:'请输入菜单Url',trigger: 'blur'}]">
              <el-input v-model="editForm.path" autocomplete="off" clearable
                        placeholder="输入格式 /xx/x"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="菜单组件" prop="component" label-width="100px"
                          :rules="this.editForm.type === 0?[{required: false}]:[{required: true,message:'请输入组件地址',trigger: 'blur'}]">
              <el-input v-model="editForm.component" autocomplete="off" clearable
                        placeholder="输入组件在项目中的地址 格式 x/x"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span=12>
            <el-form-item label="排序号" prop="ordernum">
              <el-input-number v-model="editForm.ordernum" :min="1" label="排序号">1</el-input-number>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="状态" prop="statu" label-width="100px">
              <el-radio-group v-model="editForm.statu">
                <el-radio :label=0>禁用</el-radio>
                <el-radio :label=1>正常</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="类型" prop="type" label-width="100px">
              <el-radio-group v-model="editForm.type">
                <el-radio :label=0>目录</el-radio>
                <el-radio :label=1>菜单</el-radio>
                <el-radio :label=2>按钮</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>


        </el-row>

        <el-row>
          <el-col :span="12" style="text-align: right;padding-right: 10px">
            <el-button type="primary" @click="submitForm('editForm')">提交</el-button>
          </el-col>
          <el-col :span="12" style="text-align: left; padding-left: 10px">
            <el-button @click="resetForm('editForm')">重置</el-button>
          </el-col>

        </el-row>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import {save, getItemById} from "@/api/sys/menu";

export default {
  name: "Menu-Dialog",
  data() {
    return {
      editForm: {
        parentId: "",
        name: "",
        perms: "",
        icon: '',
        path: "",
        component: "",
        ordernum: 0,
        type: 1,
        statu: 1
      },
      tableData: {},
      dialogVisible: false,
      loading: false,
      //校验规则
      editFormRules: {
        parentId: [{required: false, message: '请输入名称', trigger: 'blur'}],
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        perms: [
          {required: true, message: '请输入权限编码', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        ordernum: [
          {required: true, message: '请填入排序号', trigger: 'blur'}
        ],
        statu: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    //提交表单
    submitForm() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.loading = true
          //  检验成功
          if (this.editForm.parentId === "") {
            this.editForm.parentId = 0
          }
          const data = this.editForm
          save(data).then(res => {
            this.loading = false
            this.dialogVisible = false
            this.$emit("handle", res)
            this.$message({
              type:'success',
              message:"操作成功"
            })
          })
        }
      })
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      // this.dialogVisible = false
      this.editForm = {
        parentId: "",
        name: "",
        perms: "",
        icon: '',
        path: "",
        component: "",
        ordernum: 0,
        type: 0,
        statu: 1
      }
    },
    //父组件调用 设置信息
    setTableData(data) {
      this.tableData = data
    },
    setEditForm(id) {
      getItemById(id).then(res => {
        this.editForm = res.data
      })
      // this.editForm = id
    },
    //对话框关闭处理
    handleClose() {
      this.dialogVisible = false
      this.resetForm('editForm')
    },
    //打开对话框
    openDialog() {
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped>

</style>
